package Classes.GameViews.ViewCartoonTargets// ActionScript file
{
	import Classes.CustomEvents.CustomEvent;
	import Classes.Interfaces.I_NeedUpdatePaperMan;
	import Classes.Managers.M_CommonZombieCartoons;
	
	import MV_BMP.BMPMCEvent;
	import MV_BMP.MovieClip_BMP;
	
	import flash.events.Event;
	import flash.geom.Point;

	public class VCT_CommonZombie extends ViewCartoonTarget implements I_NeedUpdatePaperMan
	{
		private var _zombieType:String;
		private var _nowActionName:String;
		private var _nowDir:String;
		public function VCT_CommonZombie(zombieType:String):void
		{
			super();
			this._zombieType=zombieType;
			this.init();
		}
		
		private function init():void
		{
			this.setCartoons(M_CommonZombieCartoons.instance.getZombieCartoonsObj(this.zombieType,this));
			this.addListener();
		}
		
		private function addListener():void
		{
			this.addEventListener(BMPMCEvent.LAST_FRAME,lastFrameHandler);
		}
		
		private function removeListener():void
		{
			this.removeEventListener(BMPMCEvent.LAST_FRAME,lastFrameHandler);
		}
		
		private function lastFrameHandler(e:Event):void
		{
			if(this.nowActionName=="attack" || this.nowActionName=="beHitted" || this.nowActionName=="die")
			{
				this.dispatchEvent(new CustomEvent(CustomEvent.CARTOONPLAYOVER,this.nowActionName,true))
			}
		}
		
		public function updatePaperMan():void
		{
			this.setCartoons(M_CommonZombieCartoons.instance.getZombieCartoonsObj(this.zombieType,this),true);
		}
		
		public function playAction(dir:String,actionName:String):void
		{
			switch(dir)
			{
				case "up":
					dir="leftUp";
					break;
				case "right":
					dir="rightUp";
					break;
				case "down":
					dir="rightDown";
					break;
				case "left":
					dir="leftDown";
					break;
			}
			if(this.nowDir==dir && this.nowActionName==actionName)return;
			var isMirror:Boolean=false;
			this._nowDir=dir;
			this._nowActionName=actionName;
			if(dir=="leftUp")
			{
				isMirror=true;
				dir="rightUp";
			}
			else if(dir=="rightDown")
			{
				isMirror=true;
				dir="leftDown";
			}
			this.playCartoon(actionName+"_"+dir,isMirror);
		}
		
		public function hitTest(point:Point):Boolean
		{
			var checkTarget:MovieClip_BMP=this.getChildAt(0) as MovieClip_BMP;
			if(checkTarget==null)return false;
			if(this.nowDir=="leftUp" || this.nowDir=="rightDown")
			{
				point.x+=checkTarget.width;
			}
			return checkTarget.hitTest(point);
		}
		
		override public function release():void
		{
			this.removeListener();
			super.release();
		}

		/*getter & setter*/
		public function get zombieType():String
		{
			return _zombieType;
		}

		public function get nowDir():String
		{
			return _nowDir;
		}

		public function get nowActionName():String
		{
			return _nowActionName;
		}


	}
}